package com.vancouver.knows.business.service.impl;


import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.pangu.service.ActionException;
import com.pangu.service.impl.ProtocolWrap;
import com.pangu.util.StringUtil;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.vancouver.knows.business.bean.NameValuePair;
import com.vancouver.knows.business.cache.PersistentCacheManager;


/**
 * 与接口交互的协议数据及请求封装
 * <p/>
 * Created by cxlin
 */
public class BProtocolFactory {

	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
	public static final MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg");

	/**
	 * 生成用户登陆请求信息
	 * 
	 * @param password
	 * @param gcmToken
	 * @return
	 */
	public ProtocolWrap getLoginRequest(String email, String phone, String password, String gcmToken) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_login");
		if (!StringUtil.isEmptyString(phone)) {
			jsonObject.put("phone_number", phone);
		}
		if (!StringUtil.isEmptyString(email)) {
			jsonObject.put("email", email);
		}

		jsonObject.put("user_password", password);
		if (!StringUtil.isEmptyString(gcmToken)) {
			jsonObject.put("device_token", gcmToken);
		}
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);

		return pw;
	}

	/**
	 * 查询用户
	 * @param userID  用户id
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap getQueryUserInfoRequest(String userID) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_info");

		if (!StringUtil.isEmptyString(userID)) {
			jsonObject.put("user_id", userID);
		}

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);

		return pw;
	}

	/**
	 * 修改密码
	 * @param phone 手机号
	 * @param email 邮箱
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap getForgotPasswordRequest(String phone, String email) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "forgot_password");

		if (!StringUtil.isEmptyString(phone)) {
			jsonObject.put("phone_number", phone);
		}
		if (!StringUtil.isEmptyString(email)) {
			jsonObject.put("email", email);
		}

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);

		return pw;
	}

	/**
	 * 修改密码
	 * @param phone 手机号
	 * @param email 邮箱
	 * @param verificationCode  6位数验证码
	 * @param userPassword 用户输入的新密码
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap getModifyPasswordRequest(String phone, String email, String verificationCode,
			String userPassword) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "reset_password");

		if (!StringUtil.isEmptyString(phone)) {
			jsonObject.put("phone_number", phone);
		}
		if (!StringUtil.isEmptyString(email)) {
			jsonObject.put("email", email);
		}
		jsonObject.put("verification_code", verificationCode);
		jsonObject.put("user_password", userPassword);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);

		return pw;
	}

	/**
	 * 邮箱验证码
	 * @param email 邮箱
	 * @throws ActionException
	 */
	public ProtocolWrap emailSendSecurityCode(String email) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "email_validate");
		jsonObject.put("email", email);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 邮箱注册
	 * @param email 邮箱
	 * @param verificationCode 验证码
	 * @param userNickName 昵称
	 * @param password 密码
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap emailUserRegister(String email, String verificationCode, String userNickName, String password) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_registration");
		jsonObject.put("email", email);
		jsonObject.put("verification_code", verificationCode);
		jsonObject.put("user_nickname", userNickName);
		jsonObject.put("user_password", password);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 获取手机验证码
	 * @param phone 手机号
	 * @throws ActionException
	 */
	public ProtocolWrap phoneSendSecurityCode(String phone) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "phone_validate");
		jsonObject.put("phone_number", phone);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 手机注册
	 * @param phone 手机号码
	 * @param verificationCode 验证吗
	 * @param userNickName 用户昵称
	 * @param password 密码
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap phoneUserRegister(String phone, String verificationCode, String userNickName, String password) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_registration");
		jsonObject.put("phone_number", phone);
		jsonObject.put("verification_code", verificationCode);
		jsonObject.put("user_nickname", userNickName);
		jsonObject.put("user_password", password);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 搜索
	 * @param keyWord 关键字
	 * @param fromType 搜索入口
	 * @param from 请求页
	 * @param size 请求页中个数
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap userSearch(String keyWord, String fromType, String from, String size, String complete) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_search");
		jsonObject.put("keyword_string", keyWord);

		if (!StringUtil.isEmptyString(fromType)) {
			jsonObject.put("post_type", fromType);
		}
		if (!StringUtil.isEmptyString(from)) {
			jsonObject.put("from", from);
		}
		if (!StringUtil.isEmptyString(size)) {
			jsonObject.put("size", size);
		}
		if (!StringUtil.isEmptyString(complete)) {
			jsonObject.put("complete_search", complete);
		}
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 首页
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap homePage(String version) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "new_home");
		jsonObject.put("platform", "android");
		jsonObject.put("version", version);

		String checksum = (String) PersistentCacheManager.getInstance().getObject(PersistentCacheManager.HOMEPAGE_CHECKSUM_KEY);
		if (!StringUtil.isEmptyString(checksum)) {
			jsonObject.put("checksum", checksum);
		}
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 发表文章
	 * @param userId 用户id
	 * @param title 标题
	 * @param content 内容
	 * @param thumbnail 截图
	 * @param attachmentImage 上传的图片
	 * @param category 类型
     * @return
     */
	public ProtocolWrap userPostArticle(String userId, String title, String content, String thumbnail,
			List<String> attachmentImage, String category) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_post_article");
		jsonObject.put("user_id", userId);
		jsonObject.put("post_title", title);
		jsonObject.put("post_content", content);
		jsonObject.put("post_category", category);
		if (!StringUtil.isEmptyString(thumbnail)) {
			jsonObject.put("post_thumbnail", thumbnail);
		}
		if (attachmentImage != null && attachmentImage.size() > 0) {
			jsonObject.put("post_attachment_image", attachmentImage);
		}

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	public ProtocolWrap uploadUserPost(String userId, String postId, String content, String thumbnail, List<String> imgUrls, String status,
									   String category, String subcategory, String price, String city, String phone, String email) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "userpost_edit");
		jsonObject.put("user_id", userId);
		if (!StringUtil.isEmptyString(postId)) {
			jsonObject.put("post_id", postId);
		}
		jsonObject.put("post_content", content);
		if (!StringUtil.isEmptyString(thumbnail)) {
			jsonObject.put("post_thumbnail", thumbnail);
		}
		if (imgUrls != null && imgUrls.size() > 0) {
			jsonObject.put("post_attachment_image", imgUrls);
		}
		jsonObject.put("post_status", status);
		jsonObject.put("post_category", category);
		jsonObject.put("post_sub_category", subcategory);
		if (!StringUtil.isEmptyString(price)) {
			jsonObject.put("userpost_price", price);
		}
		jsonObject.put("userpost_city", city);
		jsonObject.put("userpost_phone", phone);
		jsonObject.put("userpost_email", email);
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 用户图片更新
	 * @param userId 用户id
	 * @param imageUrl 需更新的图片url
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap userUploadImage(String userId, String imageUrl) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_add_avatar");
		jsonObject.put("user_id", userId);
		jsonObject.put("image_url", imageUrl);

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 添加用户GCM的token
	 * @param userId 用户id
	 * @param registrationId gcm注册id
	 * @param status 状态
	 * @param auid auid
     * @return
     */
	public ProtocolWrap addUserToken(String userId, String registrationId, String status,String auid) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_add_token");
		if (!StringUtil.isEmptyString(userId)) {
			jsonObject.put("user_id", userId);
		}
		if (!StringUtil.isEmptyString(status)) {
			jsonObject.put("token_status", status);
		}
		if (!StringUtil.isEmptyString(auid)) {
			jsonObject.put("auid", auid);
		}
		jsonObject.put("registration_id", registrationId);
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 添加用户FCM的token
	 * @param userId
	 * @param fcmToken
     * @return
     */
	public ProtocolWrap addUserToken(String userId, String fcmToken) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_firebase_token");
		if (!StringUtil.isEmptyString(userId)) {
			jsonObject.put("user_id", userId);
		}
		if (!StringUtil.isEmptyString(fcmToken)) {
			jsonObject.put("firebase_token", fcmToken);
		}

		jsonObject.put("platform", "Android");

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 上传推送设置
	 * @param userId 用户id
	 * @param postReminder 文章评论推送
	 * @param commentReminder at我的评论推送
	 * @param businessReminder 关注商家推送
     * @return
     */
	public ProtocolWrap setMessageRemind(String userId, boolean postReminder, boolean commentReminder,
			boolean businessReminder) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_set_notifications");
		jsonObject.put("user_id", userId);
		jsonObject.put("post_reminder", postReminder);
		jsonObject.put("comment_reminder", commentReminder);
		jsonObject.put("business_reminder", businessReminder);
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 *  获取用户推送设置
	 * @param userId 用户id
	 * @return
     */
	public ProtocolWrap userGetNotificationSetting(String userId) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_get_notification_setting");
		jsonObject.put("user_id", userId);
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 获取auid
	 * @param androidId
	 * @param imei
	 * @param adid
     * @return
     */
	public ProtocolWrap userGetAuid(String androidId,String imei,String adid) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_get_auid");
		jsonObject.put("android_id", androidId);
		jsonObject.put("imei", imei);
		jsonObject.put("adid", adid);
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 获取 userIdHash
	 * @param userId
	 * @param auid
	 * @return
	 */
	public ProtocolWrap userHash(String userId,String auid) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "user_hash");
		if (!StringUtil.isEmptyString(userId)) {
			jsonObject.put("user_id", userId);

		} else if (!StringUtil.isEmptyString(auid)) {
			jsonObject.put("auid", auid);
		}

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 版本更新
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap updateVersion() {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "get_android_version");

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	/**
	 * 开屏广告 splash Ad
	 * @return
	 * @throws ActionException
	 */
	public ProtocolWrap sendSplashAd() {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "splash_ad");

		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}

	public ProtocolWrap userBusinessPhotos(String userId, String postId, List<String> imgUrls, String caption) {
		ProtocolWrap pw = new ProtocolWrap();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("page", "add_business_image");
		jsonObject.put("user_id", userId);
		jsonObject.put("post_id", postId);
		jsonObject.put("images_array", imgUrls);
		if (!StringUtil.isEmptyString(caption)) {
			jsonObject.put("images_caption", caption);
		}
		RequestBody body = RequestBody.create(JSON, jsonObject.toJSONString());
		pw.setPostBody(body);
		pw.setIsHttps(true);
		return pw;
	}
}